package com.rdzn.mall.message.vo;

import com.rdzn.mall.message.param.TemplateParam;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * @author zhigangding
 * @date 2020/2/25
 */
@Data
public class Template implements Serializable {

    private static final long serialVersionUID = -4191140084555792567L;

    private String touser;
    private String templateId;
    private String page;
    private List<TemplateParam> templateParamList;

    /**
     * {"touser":"ousMF5sdnLzkvUfJUyvbtsqnT2XM","template_id":"nfNFW6Gn6Icx2MviKoHMkYMtyGv-iU7X5eIjlekLD0s","page":"pages/index/index",
     * "data":{"thing1": {"value":"商品信息"},"amount2": {"value":"0.01"},"character_string3": {"value":"11"},"time4": {"value":"2020-02-25 10:04:58"},"thing6": {"value":"备注"}}}
     * @return
     */
//    @Override
    public String toJSON() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("{");
        buffer.append(String.format("\"touser\":\"%s\"", this.touser)).append(",");
        buffer.append(String.format("\"template_id\":\"%s\"", this.templateId)).append(",");
        buffer.append(String.format("\"page\":\"%s\"", this.page)).append(",");
        buffer.append("\"data\":{");
        TemplateParam param = null;
        for (int i = 0; i < this.templateParamList.size(); i++) {
            param = templateParamList.get(i);
            // 判断是否追加逗号
            if (i < this.templateParamList.size() - 1){
                buffer.append(String.format("\"%s\": {\"value\":\"%s\"},", param.getKey(), param.getValue()));
            }else{
                buffer.append(String.format("\"%s\": {\"value\":\"%s\"}", param.getKey(), param.getValue()));
            }
        }
        buffer.append("}");
        buffer.append("}");
        return buffer.toString();
    }
}
